package com.ysxsoft.wdg.ui.activity.my.jf;

import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.classic.common.MultipleStatusView;
import com.umeng.socialize.UMShareAPI;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.CollectShopResponse;
import com.ysxsoft.wdg.bean.response.JfResponse;
import com.ysxsoft.wdg.bean.response.PinDetailResponse;
import com.ysxsoft.wdg.bean.response.StPayResponse;
import com.ysxsoft.wdg.bean.response.WxResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.pay.PayListenerUtils;
import com.ysxsoft.wdg.pay.PayPopWindow2;
import com.ysxsoft.wdg.pay.PayResultListener;
import com.ysxsoft.wdg.pay.PayUtils;
import com.ysxsoft.wdg.pay.WxPayBean;
import com.ysxsoft.wdg.ui.activity.order.AplyBackOrderActivity;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ListObserverMap;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.widget.SharePopupWindow;
import com.ysxsoft.wdg.widget.alertview.AlertUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

public class JfOrderDetailActivity extends BaseActivity {

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_address_name)
    TextView tvAddressName;
    @BindView(R.id.tv_address_phone)
    TextView tvAddressPhone;
    @BindView(R.id.tv_address_address)
    TextView tvAddressAddress;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv_goods_money)
    TextView tvGoodsMoney;
    @BindView(R.id.tv_yun)
    TextView tvYun;
    @BindView(R.id.tv_total_money)
    TextView tvTotalMoney;
    @BindView(R.id.parent)
    RelativeLayout parent;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_pay_time)
    TextView tvPayTime;
    @BindView(R.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private String phone = "", orderid = "", uid = "";
    private ZyOrderDetailAdapter adapter;
    private String mobile="";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jf_duihuan_order_detail;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("订单详情");

        uid = SharePrefUtils.getUserId();
        orderid = getIntent().getStringExtra("orderid");

        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setFocusable(false);
        adapter = new ZyOrderDetailAdapter(R.layout.include_order_goods);
        recyclerview.setAdapter(adapter);
//        bean= (PinOrderResponse.DataBean) getIntent().getSerializableExtra("data");
//        fillView(bean);
        getDetail(orderid);
    }

    /**
     * 填充视图
     */
    private void fillView(JfOrderDetailResponse.DataBean d) {

        //1.订单状态
        int type = 0;

//        PinDetailResponse.DataBean.OrderBean bean = d.get();

        if (d.getStatus() == 0) {//0未支付1支付成功
            tvStatus.setText("未发货");
        } else {//支付成功
            tvStatus.setText("已发货");
        }
        //2.收货人地址
        tvAddressName.setText(d.getAddress().getName());
        tvAddressPhone.setText(d.getAddress().getPhone());
        tvAddressAddress.setText(d.getAddress().getCity() + d.getAddress().getProvince() + d.getAddress().getCounty() + d.getAddress().getAddress());
        //3.填充商品
        PinDetailResponse.DataBean.DetailBean item = new PinDetailResponse.DataBean.DetailBean();
       item.setPic(d.getPic());
       item.setProduct_name(d.getName());
       item.setRule_name("");
       item.setL_price(d.getScore());
       item.setNum(1);

        List<PinDetailResponse.DataBean.DetailBean> datas = new ArrayList<>();
        datas.add(item);

        adapter.setNewData(datas);//填充商品
        //4.商品金额
        tvGoodsMoney.setText("￥" + d.getPrice());
        //5.运费
//        tvYun.setText("￥" + d.get);
        //6.订单总价
        tvTotalMoney.setText("￥" + d.getPrice());
        //7.联系客服
        mobile = d.getServer();
        //8.订单详情
        tvOrderNo.setText("订单编号：" + d.getDsn());
        tvOrderTime.setText("订单时间：" + d.getAddtime());
        tvPayTime.setText("支付时间："+d.getAddtime());
        tvPayType.setText("支付方式：积分兑换");
    }

    @Override
    protected void setListener() {
    }

    @OnClick({R.id.title_finish,R.id.tv_kf})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.tv_kf:
                if (TextUtils.isEmpty(mobile)) {
                    ToastUtils.showToast("客服电话为空！");
                    return;
                }
                ViewUtils.call(this, mobile);
                break;
        }
    }

    class ZyOrderDetailAdapter extends BaseQuickAdapter<PinDetailResponse.DataBean.DetailBean, BaseViewHolder> {
        public ZyOrderDetailAdapter(int layoutResId) {
            super(layoutResId);
        }

        public void clear() {
            getData().clear();
            notifyDataSetChanged();
        }

        @Override
        protected void convert(BaseViewHolder helper, PinDetailResponse.DataBean.DetailBean item) {
            ViewUtils.loadImage(mContext, item.getPic() == null ? "" : item.getPic(), (ImageView) helper.getView(R.id.iv_goods));
            helper.setText(R.id.tv_goods_name, item.getProduct_name());
            helper.setText(R.id.tv_rules, item.getRule_name());
            helper.setText(R.id.tv_money, item.getL_price()+"积分");
            helper.setText(R.id.tv_num, "x" + item.getNum());
        }
    }

    /**
     * 查询订单详情
     */
    private void getDetail(String orderId) {
        OkHttpUtils.post()
                .url(UrlHost.JF_ORDER_DETAIL)
                .addParams("orderid", orderId)//订单id
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    JfOrderDetailResponse bean = JsonUtils.parseByGson(response, JfOrderDetailResponse.class);
                    if (bean == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(bean.getCode())) {
                        fillView(bean.getData());
                    } else {
                        ToastUtils.showToast(bean.getMs());
                    }
                } else {
                }
            }
        });
    }
}
